﻿using Shumei.Common;
using Shumei.EFCore.Model;
using Shumei.EFCore.SimpleService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace source.Areas.Admin.Controllers
{
    public class NewsController:Controller
    {
        NewsService newsService;
        public NewsController()
        {
            newsService = new NewsService();
        }
        /// <summary>
        /// 获取新闻栏目
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCategory()
        {
            News_CategoryService news_categoryService = new News_CategoryService();
            return Json(news_categoryService.getList(),JsonRequestBehavior.AllowGet);
        }

        
        /// <summary>
        /// 获取新闻列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public ActionResult NewsList(int page=1,int num = 10)
        {
            int totalPage;
            var res = newsService.GetNewsList(page, out totalPage,num);
            ViewBag.page = page;       
            ViewBag.totalPage = totalPage;
            return View("NewsList",res);
        }
        /// <summary>
        /// 打开UpLoad页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadNews()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddNews(New newModel)
        {
            newModel.UploadTime = DateTime.Now;
            newModel.user = AccountController.GetUserInfo(this.HttpContext);
            try
            {
                newsService.Add(newModel, true);
                return RedirectToAction("NewsList");
            }
            catch
            {

            }
            return null;

        }

        [HttpGet]
        public ActionResult EditNew(int id)
        {
            var _new =  newsService.GetNewDetail(id);
            if(_new!=null)
            {
                return View("EditNews", _new);
            }
            return null;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditNew(New model)
        {
            
            newsService.Update(model, true);
            return RedirectToAction("NewsList");
        }

        [HttpGet]
        public ActionResult DeleteNew(int id)
        {
            try
            {
                var res = newsService.GetNewDetail(id);
                if (res != null)
                    newsService.Delete(res);
                return RedirectToAction("NewsList");
            }
            catch
            {

            }
            return null;
        }
    }
}